package cn.kgc.microservicecloudconsumerdept80.controller;

import cn.kgc.microservicecloudapi.pojo.BookCategory;
import cn.kgc.microservicecloudapi.pojo.ResultEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer/category")
public class CategoryController {
    private static final String REST_URL_PREFIX="http://MICROSERVICECLOUD-PROVIDER-DEPT/provider/category";
    @Resource
    RestTemplate restTemplate;

    @RequestMapping("/list")
    public ResultEntity<List<BookCategory>> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/list",ResultEntity.class);
    }

    @RequestMapping("/get/{id}")
    public ResultEntity<BookCategory> get(@PathVariable Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/get/"+id,ResultEntity.class);
    }

    @RequestMapping("/update")
    public ResultEntity<String> update(@RequestBody BookCategory category){
        return restTemplate.postForObject(REST_URL_PREFIX+"/update",category,ResultEntity.class);
    }

    @RequestMapping("/insert")
    public ResultEntity<String> insert(@RequestBody BookCategory category){
        return restTemplate.postForObject(REST_URL_PREFIX+"/insert",category,ResultEntity.class);
    }

    @RequestMapping("/delete/{id}")
    public ResultEntity<String> delete(@PathVariable Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/delete/"+id,ResultEntity.class);
    }

}
